<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/17
 * Time: 14:22
 */

namespace Tools;
class paged
{
        //表的名称，[每页的行数，模糊查询的操作（传入要查询的内容），要查询的字段名字，自定义的where sql语句（搜索的条件）](后面4个都有默认值可以不填的)
    function paged($which_table, $listRow = 6, $find_goods = null, $table_name = array(), $str = null)
    {
        $hot_goods = D($which_table);
        $num = $hot_goods->count();
        $pages = new \Tools\page($num, $listRow);
        $arr = array();
        if ($find_goods) {
            $str == null ? $str = null : $str .= " and ";
            for ($i = 0; $i < count($table_name); $i++) {
                if ($i == count($table_name) - 1) {
                    $str .= $table_name[$i] . " like '%" . $find_goods . "%'";
                } else {
                    $str .= $table_name[$i] . " like '%" . $find_goods . "%' or ";
                }
            }
            $arr = $this->special_paged($hot_goods, $pages, $str, $listRow);
        } else {
            if ($str != null) {
                $arr = $this->special_paged($hot_goods, $pages, $str, $listRow);
            } else {
                $result = $hot_goods->limit($pages->limit)->select();
                $arr['result'] = $result;
                $arr['page'] = $pages;
                $arr['num'] = $num;
            }
        }
        return $arr;
    }

    //特殊分页
    function special_paged($hot_goods, $pages, $str, $listRow)
    {
        $num = $hot_goods->where($str)->count();
        $pages->total = (int)$num;
        $pages->listRows = (int)$num >= $listRow ? $listRow : (int)$num;
        $pages->pageNum = ceil($pages->total / $pages->listRows);
        $result = $hot_goods->where($str)->limit($pages->limit)->select();
        $arr = array();
        $arr['result'] = $result;
        $arr['page'] = $pages;
        $arr['num'] = $num;
        return $arr;

    }
}